package com.leapp.sk.viewdelegate.personal

import android.content.Context
import android.content.Intent
import android.support.v4.content.ContextCompat
import android.support.v4.widget.SwipeRefreshLayout
import android.text.TextUtils
import com.example.inter.ListViewRefreshInterface
import com.example.inter.RefreshLoadMoreInferface
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.RefreshListView
import com.example.mvp.view.SKTopBar
import com.leapp.sk.R
import com.leapp.sk.bean.FeedBackEntity
import com.leapp.sk.ui.activity.personal.FeedBackDetialActicity
import com.leapp.sk.ui.activity.personal.FeedbackActivity
import com.leapp.sk.ui.adapter.FeedBackAdapter
import com.example.util.finallist.FEED_BACK_ID

/**
 * 意见反馈视图托管类
 * @author  chaohaipeng  on 2018/8/9.
 */
class FeedBackDelegate :BaseAppDelegate(){
    /**
     * 适配器
     */
    private var mAdapter:FeedBackAdapter?=null
    /**
     * 刷新接口
     */
    private var listViewRefreshInterface: ListViewRefreshInterface? = null
    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_feed_back
    }

    /**
     *初始化适配器
     */
    fun initAdapter(context: Context, mlist: List<FeedBackEntity>,refreshLoadMoreInferface: RefreshLoadMoreInferface,listener: SwipeRefreshLayout.OnRefreshListener){
        val lv_afb_list = getView<RefreshListView>(R.id.lv_afb_list)
        lv_afb_list.initFooterView(context, refreshLoadMoreInferface)
        listViewRefreshInterface = lv_afb_list.getListViewRefreshInterface()
        val srl_afb_swipe = getView<SwipeRefreshLayout>(R.id.srl_afb_swipe)
        mAdapter = FeedBackAdapter(context, mlist)
        lv_afb_list.adapter=mAdapter
        srl_afb_swipe.setOnRefreshListener(listener)
        srl_afb_swipe.setColorSchemeColors(ContextCompat.getColor(context, R.color.color_19AD19))
        lv_afb_list.setOnItemClickListener{adapterView, view, i, l ->
            try {
                val intent=Intent(context, FeedBackDetialActicity::class.java)
                val item:FeedBackEntity = mAdapter!!.getItem(i) as FeedBackEntity
                intent.putExtra(FEED_BACK_ID,item.id)
                context.startActivity(intent)
            }catch (e:Exception){

            }

        }
    }
    /**
     * 停止刷新
     */
    fun stopSwipRefresh(){
        val srl_afb_swipe = getView<SwipeRefreshLayout>(R.id.srl_afb_swipe)
        srl_afb_swipe.isRefreshing=false
    }
    /**
     * 设置加载数据
     */
    fun setLoadPageData(currentPage:Int,totalPage:Int,totalCount:Int){
        if (listViewRefreshInterface != null) {
            listViewRefreshInterface?.loadMore(currentPage, totalPage, totalCount)
        }

    }

    /**
     * 刷新适配器
     */
    fun notifyAdapter(){
        mAdapter?.notifyDataSetChanged()

    }


    /**
     * 设置标题栏
     */
    fun setTitle(title:String?){
        val jtb_afb= getView<SKTopBar>(R.id.jtb_afb)
        if (!TextUtils.isEmpty(title)){
            jtb_afb.setTitle(title)
        }
        setEvent(jtb_afb)
    }

    /**
     * 设置设置事件
     */
    fun setEvent(topBar: SKTopBar){
        topBar?.setEventInterface(object : SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<FeedbackActivity>().finish()

            }
            override fun rightOnClick() {
            }

        })
    }
}